home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / ifish / mediaimp.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  9.9 KB  |  348 lines  |  [TEXT/ttxt]

  1. --<<<-
  2. -- Filename:
  3. --    mediaimp.sx
  4.  
  5. -- Other Files Required:
  6. --    This file is not part of any specific example, but rather a useful class for
  7. --    importing media and storing the media into ObjectStore.
  8.  
  9. -- Purpose:
  10. --     Provide a layer to the import export engine which enables easy use of ObjectStore. 
  11.  
  12. -- Specialized Classes:
  13. --     MediaImporter
  14.  
  15. -- Instructions to User:
  16. --    Class MediaImporter provides a layer to the import export engine
  17. --    which enables easy use of ObjectStore. When you create a MediaImporter,
  18. --    you may specify a storage container in which media is automatically stored,
  19. --    and a target collection to which media is automatically added. The target 
  20. --    must be an ExplicitKeyedCollection subclass (e.g. HashTable). When you are
  21. --    done importing, you can invoke the MediaImporter saveMedia method to register
  22. --    the hash table with the storage container, and then store the hash table.
  23. --    For each import method, you specify a fileName and optional mediaKey. The
  24. --    import adds an entry into the ExplicitKeyedCollection using the filename as
  25. --    the key, unless the mediaKey is supplied. In this case, mediaKey is used as
  26. --    the key.
  27.  
  28. --    PUBLIC PROTOCOL
  29. --    importObject self fileName key:mediaKey
  30. --    importDIB self fileName key:mediaKey
  31. --    importPICT self fileName key:mediaKey
  32. --    importRTF self fileName key:mediaKey
  33. --    importAIFF self fileName key:mediaKey
  34. --    importWave self fileName key:mediaKey
  35. --    importQuicktime self fileName key:mediaKey
  36. --    importRes self type id key:mediaKey
  37. --    saveMedia self name
  38.  
  39. --    KEYWORD ARGUMENTS
  40. --    path            -- path from which to import media.
  41. --    container        -- storage container for media
  42. --    target            -- target ExplicitKeyedCollection
  43. --    addExtensions    -- if true, file extensions will be added for specific media.
  44. --    extension        -- extension to add
  45. --    mediaCategory    -- @image, @sound, @movie
  46. --    inputMediaType    -- @dib, @pict, etc.
  47. --    outputMediaType    -- @bitmap, @player, etc.
  48.  
  49. --    EXAMPLE CREATION
  50. --    An importer which imports from the StartDir:
  51. --    mi := new MediaImporter
  52.  
  53. --    An importer which imports from a folder called "media", and stores media in
  54. --    storage container c/hashtable h:
  55. --    mi := new MediaImporter path:(spawn theStartDir "media") container:c target:h
  56.  
  57. -- Author:
  58. --     Steve Mayer
  59.  
  60. in module InternetFish
  61.  
  62.  
  63. class MediaImporter ()
  64. class variables
  65.     extensionMapping    -- mapping of file extensions to import parameters.
  66. instance variables
  67.     path                -- path from which to import media.
  68.     container            -- storage container for media
  69.     target            -- target ExplicitKeyedCollection
  70.     addExtensions        -- if true, file extensions will be added for specific media.
  71.     smartExtensions    -- if true, file type is inferred from extension.
  72.     extension            -- extension to add
  73.     mediaCategory        -- @image, @sound, @movie
  74.     inputMediaType    -- @dib, @pict, etc.
  75.     outputMediaType    -- @bitmap, @player, etc.
  76.     bundle            -- ResBundle instance, if we are importing Res files.
  77.     convertToShapes    -- If true, convert bitmaps to twodshapes.
  78.     matteColor        -- if defined, set bitmap's matte to this color.
  79.     invisibleColor        -- if defined, set bitmaps's invisible color to this color.
  80. class methods
  81.     method init self #rest args ->
  82.     (
  83.         apply nextMethod self args
  84.         local em := new KeyedLinkedList
  85.         add em ".dib" #(@Image, @DIB, @Bitmap)
  86.         add em ".bmp" #(@Image, @DIB, @Bitmap)
  87.         add em ".pic" #(@Image, @PICT, @Bitmap)
  88.         add em ".aif" #(@Sound, @AIFF, @Player)
  89.         add em ".wav" #(@Sound, @Wave, @Player)
  90.         add em ".avi" #(@Movie, @AVI, @Player)
  91.         add em ".mov" #(@Movie, @Quicktime, @Player)
  92.         add em ".rtf" #(@Text, @RTF, @RichText)
  93.         self.extensionMapping := em
  94.         self
  95.     )
  96. end
  97.  
  98. -- Method init saves keyword arguments in instance variables.
  99. method init self {class MediaImporter} #rest args #key \
  100.     path: (theStartDir) \
  101.     container: (undefined) \
  102.     target: (undefined) \
  103.     addExtensions: (false) \
  104.     smartExtensions: (false) \
  105.     extension: ("") \
  106.     mediaCategory: (@image) \
  107.     inputMediaType: (@dib) \
  108.     outputMediaType: (@bitmap) \
  109.     convertToShapes: (true) \
  110.     mode: (@create) ->
  111. (
  112.     apply nextMethod self args
  113.     self.path := path
  114.     self.container := container
  115.     self.target := target
  116.     self.addExtensions := addExtensions
  117.     self.smartExtensions := smartExtensions
  118.     self.extension := extension
  119.     self.mediaCategory := mediaCategory
  120.     self.inputMediaType := inputMediaType
  121.     self.outputMediaType := outputMediaType
  122.     self.matteColor := undefined
  123.     self.invisibleColor := undefined
  124.     self.convertToShapes := convertToShapes
  125.     return self
  126. )
  127.  
  128. method getOne self {class MediaImporter} key ->
  129. (
  130.     getOne self.target key
  131. )
  132.  
  133. -- Method inferMediaType parses a filename to get its extension, and then
  134. -- calls setExtension on self.
  135. method inferMediaType self {class MediaImporter} fileName ->
  136. (
  137.     -- Get extension part of filename.
  138.     local ext := new String
  139.     for i := ((size fileName) - 3) to (size fileName) do
  140.     (
  141.         append ext fileName[i]
  142.     )
  143.     setExtension self ext
  144. )
  145.  
  146. method stripExtension self {class MediaImporter} fileName ->
  147. (
  148.     -- Get prefix part of filename.
  149.     local prefx := new String
  150.     for i := 1 to ((size fileName) - 4) do
  151.     (
  152.         append prefx fileName[i]
  153.     )
  154.     return prefx
  155. )
  156.  
  157. -- Method setExtension sets the current file extension to the specified
  158. -- value. It also automatically sets the import parameters based the
  159. -- MediaImporter.extensionMapping.
  160. method setExtension self {class MediaImporter} extension ->
  161. (
  162.     self.extension := extension
  163.     local result := MediaImporter.extensionMapping[extension]
  164.     if (result <> empty) do
  165.     (
  166.         self.mediaCategory := result[1]
  167.         self.inputMediaType := result[2]
  168.         self.outputMediaType := result[3]
  169.     )
  170. )
  171.  
  172. -- Method processBitmap is called to process an imported bitmap, based on
  173. -- MediaImporter parameters.
  174. method processBitmap self {class MediaImporter} bm ->
  175. (
  176.     if (self.invisibleColor <> undefined) do
  177.     (
  178.         bm.invisibleColor := self.invisibleColor
  179.     )
  180.     if (self.matteColor <> undefined) do
  181.     (
  182.         bm.matteColor := self.matteColor
  183.     )
  184.     if (self.convertToShapes) do
  185.     (
  186.         bm := new TwoDShape boundary:bm fill:blackBrush
  187.     )
  188.     return bm
  189. )
  190.  
  191. -- Method storeMedia handles adding the media to the storage container
  192. -- and target keyed collection, if either has been specified.
  193. method storeMedia self {class MediaImporter} name media ->
  194. (
  195.     -- proxy for media if using a storage container.
  196.     local px 
  197.     if self.container <> undefined do
  198.     (
  199.         -- Register the media with the storage container.
  200.         px := addToStorageContainer media self.container
  201.         store px
  202.         makePurgeable px
  203.     )
  204.     if self.target <> undefined do
  205.     (
  206.         -- Add the media to the explicit keyed collection.
  207.         if self.container <> undefined then
  208.         (
  209.             -- Store the proxy if using a storage container.
  210.             setOne self.target name px
  211.         ) else
  212.         (
  213.             -- Store the media if not using a storage container.
  214.             setOne self.target name media
  215.         )
  216.     )
  217. )
  218.  
  219. method importObject self {class MediaImporter} fileName #key mediaKey: (undefined) \
  220.     mediaStream:(undefined) ->
  221. (
  222.     local mStream
  223.     local mKey := fileName
  224.     if (mediaStream <> undefined) then
  225.     (
  226.         mStream := mediaStream
  227.     ) else
  228.     (
  229.         -- Create a media stream for the specified path, and convert to output media type.
  230.         local fullName
  231.         if (self.addExtensions) then
  232.         (
  233.             fullName := fileName + self.extension
  234.         )
  235.         else (
  236.             fullName := fileName
  237.         )
  238.         if (self.smartExtensions) do
  239.         (
  240.             -- Infer media type, and strip extension from filename.
  241.             inferMediaType self fullName
  242.             mKey := stripExtension self fullName
  243.         )
  244.         -- Build path for Media folder and append fullName.
  245.         local p := copy (self.path as Sequence)
  246.         append p fullName
  247.         mStream := getStream theRootDir p @Readable
  248.     )
  249.     local media := importMedia theImportExportEngine mStream self.mediaCategory self.inputMediaType self.outputMediaType \
  250.         container:self.container colorMap: defaultColorMap
  251.     -- If a mediaKey is specified, use it.
  252.     if (mediaKey <>  undefined) do mKey := mediaKey
  253.     
  254.     -- If it is a bitmap, call the processBitmap method.
  255.     if (self.mediaCategory = @Image) do
  256.     (
  257.         media := processBitmap self media
  258.     )
  259.     -- Store the media.
  260.     storeMedia self mKey media
  261.     plug mStream
  262.     return media
  263. )
  264.  
  265. -- Method importAllMedia iterates through every file in the media importer's path,
  266. -- and calls importObject.
  267. method importAllMedia self {class MediaImporter} ->
  268. (
  269.     -- Iterate through all files in the path.
  270.     local failList := new Array
  271.     local theErr := @all
  272.     -- Iterate through all of the files.
  273.     for fileName in (getContents self.path) do
  274.     (
  275.         -- If it is a file (not a directory)...
  276.         if (isFile self.path fileName) do
  277.         (
  278.             guard
  279.             (
  280.                 importObject self fileName
  281.             )
  282.             catching
  283.                 theErr : (
  284.                     append failList fileName
  285.                     caught theErr
  286.                     )
  287.             on exit
  288.                 undefined
  289.             end
  290.         )
  291.     )
  292. )
  293.  
  294. -- The following methods support specific media types, and use the more
  295. -- general importObject method.
  296. method importPICT self {class MediaImporter} fileName #key mediaKey: ->
  297. (
  298.     setExtension self ".pic"
  299.     return importObject self fileName mediaKey:mediaKey
  300. )
  301.  
  302. method importRTF self {class MediaImporter} fileName #key mediaKey: ->
  303. (
  304.     setExtension self ".rtf"
  305.     return importObject self fileName mediaKey:mediaKey
  306. )
  307.  
  308. method importAIFF self {class MediaImporter} fileName #key mediaKey: ->
  309. (
  310.     setExtension self ".aif"
  311.     return importObject self fileName mediaKey:mediaKey
  312. )
  313.  
  314. method importWave self {class MediaImporter} fileName #key mediaKey: ->
  315. (
  316.     setExtension self ".wav"
  317.     return importObject self fileName mediaKey:mediaKey
  318. )
  319.  
  320. method importDIB self {class MediaImporter} fileName #key mediaKey: ->
  321. (
  322.     setExtension self ".bmp"
  323.     return importObject self fileName mediaKey:mediaKey
  324. )
  325.  
  326. method importQuicktime self {class MediaImporter} fileName #key mediaKey: ->
  327. (
  328.     setExtension self ".mov"
  329.     return importObject self fileName mediaKey:mediaKey
  330. )
  331.  
  332. method importRes self {class MediaImporter} type id #key mediaKey: (undefined) ->
  333. (
  334.     setExtension self ".pic"
  335.     -- If no media key is specified, use the res file id.
  336.     local str := getOneStream self.bundle type:type ID:id
  337.     return importObject self (id as String) mediaStream:str mediaKey:mediaKey
  338. )
  339.     
  340. -- Register the target explicit keyed collection with the container, and save it.
  341. method saveMedia self {class MediaImporter} name ->
  342. (
  343.     registerPersistentRoot self.target name self.container
  344.     store self.target
  345. )
  346. -->>>
  347. "Compiled mediaimp.sx"
  348.